﻿using Newtonsoft.Json;
using Signit.Sdk;
using Signit.Sdk.Entity;
using Signit.Sdk.Http;
using Signit.Sdk.Http.Envelope;
using Signit.Sdk.Types;
using System.Collections.Generic;

namespace SdkSample.Testing
{
    class TestStartEnvelope : TestBase
    {
        public override void Test()
        {
            client.BaseUrl = $"{client.EnvUrl}/v1/open/envelopes/start";
            client.OAuthTokenUrl = $"{client.EnvUrl}/v1/oauth/oauth/token";

            // step2: 使用SDK封装实名认证请求
            StartEnvelopeRequest request = startEnvelopeWithEmbeddeMode();
            Log("Envelope Request", request);

            // step3: 执行请求,获得响应
            StartEnvelopeResponse response = null;
            try
            {
                response = client.Execute<StartEnvelopeResponse>(request);
            }
            catch (SignitException e)
            {
                ErrorResponse error = null;
                try
                {
                    JsonConvert.DeserializeObject<ErrorResponse>(e.Message);
                }
                catch
                {
                    Log("Error", e.Message);
                }

                Log("Error Object", error);
            }

            Log("Envelope Response", response);
        }
        public static StartEnvelopeRequest startEnvelopeWithEmbeddeMode()
        {
            return new StartEnvelopeRequest
            {
                Basicinfo = new EnvelopeBasicInfo
                {
                    Title = "title",
                    Subject = "subject"
                },
                ContentInfo = new EnvelopeContentInfo
                {
                    Files = new List<EnvelopeFile>
                    {
                        new EnvelopeFile
                        {
                            Id = "127090",
                            Sequence = 1,
                            Data = new BaseFileData
                            {
                                Url = "https://raw.githubusercontent.com/enHb7/Yiqiqian/master/%E5%81%87%E6%9C%9F%E7%95%99%E6%A0%A1%E4%BD%8F%E5%AE%BF%E5%AD%A6%E7%94%9F%E5%AE%89%E5%85%A8%E8%B4%A3%E4%BB%BB%E4%B9%A6%EF%BC%88%E5%AE%9E%E9%AA%8C%E5%AE%A4%E5%AD%A6%E4%B9%A0%EF%BC%89.docx"
                            },
                            IsAttached = false
                        }
                    }
                },
                ParticipantInfo = new EnvelopeParticipantInfo
                {
                    Sender = new Sender
                    {
                        Name = "刘清华",
                        Contact = new Contact
                        {
                            Phone = "18380581554"
                        },
                        DeleteCompletedEnvelope = true,
                        EnableEmbeddedMode = true,
                        ClientId = "sender_client_id1"
                    },
                    Receivers = new List<Receiver>
                    {
                        new Receiver
                        {
                            Name = "张波",
                            AssignedSequence = 1,
                            Contact = new Contact
                            {
                                Phone = "18380581554"
                            },
                            SecureLevel = SecureLevel.DISPOSABLE_CERT,
                            Type = ReceiverType.SIGNER,
                            RoleType = EnvelopeRoleType.PERSON,
                            DeleteCompletedEnvelope = true,
                            EnableEmbeddedMode = true,
                            ClientId = "receriver_client_id1",
                            SelectedAuthTypes = new List<AuthType>{ AuthType.SIGN_PIN, AuthType.SMS_CODE },
                            PresetForms = new List<PresetForm>
                            {
                                new PresetForm
                                {
                                    FileId = "127090",
                                    FormType = FormType.WRITE_SIGN,
                                    Position = new SignerPosition
                                    {
                                        RectanglePosition = new RectanglePosition
                                        {
                                            Lrx = 251f,
                                            Lry = 270f,
                                            Ulx = 151f,
                                            Uly = 220f,
                                            Page = 2
                                        }
                                    },
                                    Revisable = true,
                                    Scale = 0f
                                },
                                new PresetForm
                                {
                                    FormType = FormType.WRITE_SIGN,
                                    FileId = "127090",
                                    Position = new SignerPosition
                                    {
                                        KeywordPosition = new KeywordPosition
                                        {
                                            Width = 100f,
                                            RelativeWidthRatio = 1f,
                                            Height = 50f,
                                            RelativeHeightRatio  = 1f,
                                            Direction = Direction.RIGHT,
                                            XOffset= 0f,
                                            YOffset = 20f,
                                            Keyword = "学生签名：",
                                            Pages = "all",
                                            Index = 0
                                        }
                                    },
                                    Scale = 1f,
                                    Revisable = true
                                }
                            }
                        },
                        new Receiver
                        {
                            AssignedSequence = 2,
                            Contact = new Contact
                            {
                                Phone = "18681695956",
                            },
                            EnterpriseName = "易企签测试企业",
                            Name = "刘清华",
                            NeedForm = false,
                            RoleType = EnvelopeRoleType.ENTERPRISE_MEMBER,
                            SecureLevel = SecureLevel.DISPOSABLE_CERT,
                            Type = ReceiverType.SIGNER,
                            EnableEmbeddedMode = true,
                            ClientId = "receriver_client_id1",
                            SelectedAuthTypes = new List<AuthType>
                            {
                                AuthType.SIGN_PIN, AuthType.SMS_CODE, AuthType.EMAIL_CODE
                            },
                            PresetForms = new List<PresetForm>
                            {
                                new PresetForm
                                {
                                    FileId = "127090",
                                    FormType = FormType.SEAL_SIGN,
                                    Revisable = false,
                                    Scale = 1f,
                                    TagId = "customTagId1",
                                    Position = new SignerPosition
                                    {
                                        KeywordPosition = new KeywordPosition
                                        {
                                            Width = 70f,
                                            RelativeWidthRatio = 1.0f,
                                            Height = 70f,
                                            RelativeHeightRatio = 1.0f,
                                            Direction = Direction.RIGHT,
                                            Offset = 1.0f,
                                            RelativeOffsetRatio = 0.1f,
                                            XOffset = 0f,
                                            YOffset = 20f,
                                            Keyword = "老师签名：",
                                            Pages = "all",
                                            Index = 0,
                                        }
                                    },
                                    InitialValue = new InitialValue
                                    {
                                        RenderingMode = RenderMode.GRAPHIC,
                                        SealData = new SealData
                                        {
                                            Name = "易企签测试企业"
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                // http://webhook.site/#!/e21d6e4f-d176-487d-91e9-bc9b78bae9e5
                ReturnUrl = "http://webhook.site/e21d6e4f-d176-487d-91e9-bc9b78bae9e5",
                AcceptDataType = AcceptDataType.URL,
                CustomTag = "C130018122503922"
            };
        }
    }
}
